const TOKEN_KEY = 'token'
const USER_INFO_KEY = 'user_info'

export interface UserInfo {
  name: string
  email: string
  userCode: string
  userType: number
  access: string
  id: string
}

export const getToken = () => {
  return localStorage.getItem(TOKEN_KEY)
}

export const setToken = (token: string) => {
  localStorage.setItem(TOKEN_KEY, token)
}

export const removeToken = () => {
  localStorage.removeItem(TOKEN_KEY)
}

export const getUserInfo = (): UserInfo | null => {
  const info = localStorage.getItem(USER_INFO_KEY)
  const userInfo = info ? JSON.parse(info) : null
  return userInfo?.name ? userInfo : null
}

export const setUserInfo = (info: UserInfo) => {
  localStorage.setItem(USER_INFO_KEY, JSON.stringify(info))
}

export const removeUserInfo = () => {
  localStorage.removeItem(USER_INFO_KEY)
}

export const clearAll = () => {
  removeToken()
  removeUserInfo()
}
